<?php
/*
 * @Author: 李明
 * @Date: 2021-11-30 17:46:48
 * @LastEditTime: 2022-02-11 15:58:59
 * @LastEditors: 李明
 * @Description:
 * @FilePath: /CRM2.0-PHP/app/Http/Controllers/worker/WorkerController.php
 * @山东壹零贰肆信息科技有限公司
 */

namespace App\Http\Controllers\worker;

use App\Http\Controllers\common\CommonFun;
use App\Http\Controllers\common\LMReturn;
use App\Http\Controllers\Controller;
use App\Models\Worker;
use Illuminate\Http\Request;

class WorkerController extends Controller
{
    /**
     * @description: 员工入职接口
     * @param {Request} $request
     * @return {*}
     */
    public function newWorkerSave(Request $request)
    {
        //去验证token值
        $tokenInfo = json_decode($this->verifyTokenLm(), true);
        //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
        if ($tokenInfo['code'] == 400) {
            return LMReturn::Error($tokenInfo['msg'], $tokenInfo['code']);
        }
        // 接参
        try {
            $all = $request->all();
            $phoneNum = $all['phoneNum'];
            $is_change = isset($all['id']);
            if (!$is_change) { // 如果是增加，需要先判断 是否已存在该账号
                $worker_list = Worker::where("phoneNum", $phoneNum)->get();
                if (count($worker_list) > 0) {
                    return LMReturn::Error('账号已存在', 'WC009');
                }
            }
            $worker = $is_change ? Worker::find($all['id']) : new Worker();
            $role_list = [$all['roleinfo']];
            $branch_list = [$all['bminfo']];
            $id_type = isset($all['id_type']) ? $all['id_type'] : '1';
            $inTime = $all['inTime'];
            $id_number = isset($all['id_number']) ? $all['id_number'] : '';
            $sex = $all['sex'];
            $birthday = isset($all['birthday']) ? $all['birthday'] : '';
            $password = $is_change ? $worker->password : CommonFun::md5($all['password']);
            $contactNum = $all['contactNum'];
            $name = $all['name'];
            $user_status = isset($all['user_status']) ? $all['user_status'] : 1;
            $manage_branch_list = isset($all['manage_bm']) ? [$all['manage_bm']] : [];
        } catch (\Throwable $th) {
            return LMReturn::Error('传参失败', 'WC014');
        }
        // 修改数据库
        try {
            $worker->name = $name;
            $worker->phoneNum = $phoneNum;
            $worker->contactNum = $contactNum;
            $worker->inTime = $inTime;
            $worker->sex = $sex;
            $worker->id_type = $id_type;
            $worker->id_number = $id_number;
            $worker->password = $password;
            $worker->user_status = $user_status;
            $worker->birthday = $birthday;
            $worker->save();
            $worker->refresh();
            $worker->roles()->sync($role_list); // 替换之前的角色。
            $worker->branches()->sync($branch_list); // 替换之前的关联部门。
            $worker->branchesManage()->sync($manage_branch_list); // 替换之前的管理部门。
            return LMReturn::Success($worker, $is_change ? '修改员工成功' : '添加员工成功');
        } catch (\Throwable $th) {
            return LMReturn::Error($is_change ? '修改员工失败' : '添加员工失败', 'WC011');
        }
    }

    public function WorkerDetail(Request $request)
    {
        //去验证token值
        $tokenInfo = json_decode($this->verifyTokenLm(), true);
        //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
        if ($tokenInfo['code'] == 400) {
            return LMReturn::Error($tokenInfo['msg'], $tokenInfo['code']);
        }
        // 接参
        try {
            $all = $request->all();
            $worker_id = $all['worker_id'] ?? $tokenInfo['worker_id'];
        } catch (\Throwable $th) {
            return LMReturn::Error('接参失败', 'WC001');
        }
        // 查询数据库
        try {
            $worker = Worker::with('branches')->with('branchesManage')->find($worker_id);
            $branch_worker_power = count($worker->roles->first()->powers->where('id', '2')->all());
            $branch_user_power = count($worker->roles->first()->powers->where('id', '7')->all());
            $branch_order_power = count($worker->roles->first()->powers->where('id', '14')->all());
            $branch_manage_power = count($worker->roles->first()->powers->where('id', '1')->all());
            $pie_data_power = count($worker->roles->first()->powers->where('id', '184')->all());
            $line_data_power = count($worker->roles->first()->powers->where('id', '184')->all());
            $edit_power = count($worker->roles->first()->powers->where('id', '140')->all());
            if ($worker_id == "9192" || $worker_id == "17") {
                if (env("COMPANY", 3) == '3') {
                    $pie_data_power = 1;
                    $line_data_power = 1;
                } else if ($worker_id == "17") {
                    $pie_data_power = 1;
                    $line_data_power = 1;
                } else {
                    $pie_data_power = 0;
                    $line_data_power = 0;
                }
            } else {
                $pie_data_power = 0;
                $line_data_power = 0;
            }

            $worker_res['worker_id'] = $worker->id;
            $worker_res['user_id'] = $worker->id;
            $worker_res['worker_name'] = $worker->name;
            $worker_res['branch_id'] = $worker->branches->first()->id;
            try {
                if (count($worker->branchesManage) > 0) {
                    $manage_list = [];
                    foreach ($worker->branchesManage as $manage) {
                        $manage_list[] = $manage['id'];
                    }
                    $worker_res['manage_bm_list'] = $manage_list;
                } else {
                    $worker_res['manage_bm_list'] = [];
                }
            } catch (\Throwable $th) {}
            $worker_res['branch_manage_id'] = $worker->branchesManage->first()->id ?? null;
            $worker_res['branch_name'] = $worker->branches->first()->name;;
            $worker_res["branch_worker_power"] = $branch_worker_power;
            $worker_res["branch_user_power"] = $branch_user_power;
            $worker_res["branch_order_power"] = $branch_order_power;
            $worker_res["branch_manage_power"] = $branch_manage_power;
            $worker_res["pie_data_power"] = $pie_data_power;
            $worker_res["line_data_power"] = $line_data_power;
            $worker_res["edit_power"] = $edit_power;
            $worker_res["address"] = env('ADDRESS', '济南市历下区祥恒广场19楼');
            $worker_res["phone_num"] = $worker->contactNum;
            $worker_res["email"] = $worker->email;
            $worker_res["icon_url"] = $worker->icon;
            $worker_res["xcx_qr_code"] = 'https://api.guoyikonggu.com/images/qr_code.jpg';
            $worker_res["role_name"] = $worker->roles->first()->name;
            if ($worker->positive_time) {
                $worker_res["positive_time"] = date('Y-m-d', strtotime($worker->positive_time));
            }
            $worker_res["is_positive"] = $worker->is_positive;
            return LMReturn::Success($worker_res, '查询成功');
        } catch (\Throwable $th) {
            return LMReturn::Error('查询失败');
        }
    }
}
